package cn.iocoder.yudao.module.trade.scan.vo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.validation.InEnumValue;
import cn.iocoder.yudao.module.api.promotion.activity.enums.PromotionTypeEnum;
import cn.iocoder.yudao.module.api.promotion.coupon.dto.CouponRespDTO;
import cn.iocoder.yudao.module.api.promotion.gift.dto.GiftActivityVO;
import cn.iocoder.yudao.module.api.promotion.redpacket.vo.RedPacketUsageVO;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nonnull;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/02/25
 */
@Schema(description = "线下扫码下单计算价格明细 VO")
@Data
public class ScanOrderPreviewVO implements Serializable {

    @Schema(description = "店铺编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long shopId;

    @Schema(description = "店铺信息", requiredMode = Schema.RequiredMode.REQUIRED)
    private ShopInfoDTO shop;

    @Nonnull
    public ShopInfoDTO getShop() {
        if (shop == null) {
            shop = ShopInfoDTO.FALLBACK;
        }
        return shop;
    }

    @Schema(description = "商家的邀请码，顾客扫商家收款码付款时若注册则绑定推广关系", requiredMode = Schema.RequiredMode.REQUIRED, example = "TEST000001")
    private String inviteCode;

    @Schema(description = "原始金额，单位：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "5000")
    private int originPrice;

    @Schema(description = "结算金额，单位：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "5000")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    private Integer settlementPrice;

    @Schema(description = "应付金额，单位：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "5000")
    private int payPrice;

    @Schema(description = "1 积分抵扣多少分钱", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    private Integer pointDeductUnitPrice;

    @Schema(description = "1 收益豆抵扣多少分钱", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    @JsonIgnore // 不返回给前端，仅供后端模块内部使用
    private Integer cryptocurrencyDeductUnitPrice;

    @Schema(description = "会员减免金额，单位：分")
    private Integer vipExemptPrice;

    @Schema(description = "积分抵扣金额，单位：分")
    private Integer pointDeductPrice;

    @Schema(description = "联盟豆抵扣金额，单位：分")
    private Integer cryptocurrencyDeductPrice;

    @Schema(description = "使用的积分")
    private Integer deductPoint;

    @Schema(description = "使用的联盟豆")
    private Double deductCryptocurrency;

    @Schema(description = "红包抵扣金额，单位：分")
    private Integer redpacketDeductPrice;

    @Schema(description = "打折、优惠券的优惠金额，单位：分")
    private Integer discountPrice;

    @Schema(description = "提示信息", example = "积分不足")
    private String tips;

    @Schema(description = "优惠价格明细")
    private List<ScanPromotion> promotions;

    @Nonnull
    public List<ScanPromotion> getPromotions() {
        if (promotions == null) {
            promotions = new ArrayList<>();
        }
        return promotions;
    }

    @Schema(description = "可用的优惠券信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<CouponRespDTO> coupons;

    @Schema(description = "当前使用的优惠券信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private CouponRespDTO coupon;

    @Schema(description = "可用的红包信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private RedPacketUsageVO redpacket;

    @Schema(description = "赠品活动信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<GiftActivityVO> giftActivities;

    @Schema(description = "赠品活动编号")
    private List<Long> giftActivityIds;

    @SuppressWarnings("unused")
    public List<Long> getGiftActivityIds() {
        if (CollUtil.isEmpty(getGiftActivities())) {
            return List.of();
        }
        return getGiftActivities().stream().map(GiftActivityVO::getId).toList();
    }

    @Data
    public static class ScanPromotion implements Serializable {

        @Schema(description = "优惠类型：8-积分抵扣、9-会员价、20-收益豆抵扣、30-红包抵扣", requiredMode = Schema.RequiredMode.REQUIRED, example = "9")
        @InEnumValue(PromotionTypeEnum.class)
        private int type;

        @Schema(description = "优惠名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "会员价")
        private String name;

        @Schema(description = "优惠金额，单位：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "5000")
        private int price;

        @Schema(description = "是否匹配", requiredMode = Schema.RequiredMode.REQUIRED, example = "5000")
        private boolean match;

        @Schema(description = "优惠说明", requiredMode = Schema.RequiredMode.REQUIRED, example = "会员减免 1.00 元")
        private String note;

    }

}
